package com.yingsheng.bi.hks.service.goods;

import com.qiyuesuo.sdk.v2.json.JSONUtils;
import com.yingsheng.bi.hks.basic.PUser;
import com.yingsheng.bi.hks.basic.enums.EnumConst;
import com.yingsheng.bi.hks.basic.utils.Subject;
import com.yingsheng.bi.hks.basic.utils.Utils;
import com.yingsheng.bi.hks.comm.WxPayMpCreateOrderReq;
import com.yingsheng.bi.hks.comm.WxPayMpCreateOrderRes;
import com.yingsheng.bi.hks.dto.goods.GoodsBuyReq;
import com.yingsheng.bi.hks.dto.goods.GoodsBuyRes;
import com.yingsheng.bi.hks.repository.dao.po.Goods;
import com.yingsheng.bi.hks.repository.dao.po.GoodsOrders;
import com.yingsheng.bi.hks.repository.dao.service.GoodsDaoService;
import com.yingsheng.bi.hks.repository.dao.service.GoodsOrdersDaoService;
import com.yingsheng.bi.hks.service.comm.WxPayBiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class GoodsPayBiService {

    @Autowired
    private WxPayBiService wxPayBiService;

    @Autowired
    private GoodsDaoService goodsDaoService;

    @Autowired
    private GoodsOrdersDaoService goodsOrdersDaoService;

    public GoodsBuyRes process(GoodsBuyReq req) {
        Goods goods = goodsDaoService.selectGoodsByIdAndCheckNoNull(req.getGoodsId());
        PUser pUser = Subject.get();

        Date createDate = new Date();
        GoodsOrders goodsOrders = new GoodsOrders();
        goodsOrders.setId(Utils.genUUid());
        goodsOrders.setUserId(pUser.getId());
        goodsOrders.setPartyId(pUser.getPartyId());
        goodsOrders.setOrderState(EnumConst.GoodsOrderState.state_6);
        BigDecimal goodsBig =  new BigDecimal(goods.getPrice2().toString());
        BigDecimal count =  new BigDecimal(req.getGoodsCount());
        BigDecimal orderAmount = goodsBig.multiply(count);
        goodsOrders.setOrderAmount(orderAmount.longValue());
        goodsOrders.setGoodsId(goods.getId());
        goodsOrders.setGoodsAmount(goods.getPrice2().longValue());
        goodsOrders.setGoodsCount(req.getGoodsCount());
        goodsOrders.setPayType("WX");
        goodsOrders.setPayTime(createDate);
        goodsOrders.setAddrId(goods.getId());
        goodsOrders.setNotes(req.getNotes());
        goodsOrders.setSpecStr(JSONUtils.toJson(req.getSpecItemBos()));
        goodsOrdersDaoService.insert(goodsOrders);

        int wxAmount = orderAmount.multiply(new BigDecimal(100)).intValue();
        WxPayMpCreateOrderReq wxPayReq = new WxPayMpCreateOrderReq();
        wxPayReq.setDescription(goods.getName());
        wxPayReq.setOrderNo(goodsOrders.getId());
        wxPayReq.setAmount(wxAmount);
        wxPayReq.setOpenid(pUser.getOpenId());
        WxPayMpCreateOrderRes wxPayRes = wxPayBiService.createOrder(wxPayReq);
        GoodsBuyRes res = new GoodsBuyRes();
        res.setSignType(wxPayRes.getSignType());
        res.setNonceStr(wxPayRes.getNonceStr());
        res.setPackageValue(wxPayRes.getPackageValue());
        res.setPaySign(wxPayRes.getPaySign());
        res.setTimeStamp(wxPayRes.getTimeStamp());
        return res;
    }
}
